package self.hoy.demo.grainmall.ware.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.grainmall.ware.mapper.PurchaseDetailMapper;
import self.hoy.demo.grainmall.ware.entity.PurchaseDetail;
import self.hoy.demo.grainmall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetail> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetail> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(w -> w.eq("purchase_id", key).or().like("sku_id", key));
        }

        String status = (String) params.get("key");
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetail> page = this.page(
                new Query<PurchaseDetail>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetail> listDetailByPurchaseId(Long purchaseId) {
        return this.list(
                new QueryWrapper<PurchaseDetail>().eq("purchase_id", purchaseId)
        );
    }

}